import numpy as np


class LMSE(object):
    def __init__(self):
        self.a = None

    def train(self, y, b):
        y = np.matrix(y)
        b = np.matrix(b)
        a = (y.T * y).I * y.T * b.T
        self.a = np.array(a).reshape(-1)

    def classify(self, sample):
        return np.dot(self.a, sample) / np.linalg.norm(self.a[:-1])


if __name__ == '__main__':
    samples = np.array([
        [1, 1, 1],
        [2, 2, 1],
        [2, 0, 1],
        [0, 0, -1],
        [-1, 0, -1],
        [0, -1, -1]
    ])
    b = np.array([1 for i in range(len(samples))])

    lmse = LMSE()
    lmse.train(samples, b)

    print(lmse.a)
    for sample in samples:
        print(lmse.classify(sample))
